#-*- coding:utf-8 -*-
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django import forms
from django.forms import ModelForm

from record.models import Account

class RegistrationForm(forms.Form):
	username = forms.CharField(label='user name', max_length=30)
	email = forms.EmailField(label='e-mail')
	password1 = forms.CharField(label='password', widget=forms.PasswordInput())
	password2 = forms.CharField(label='password(retype)', widget=forms.PasswordInput())

	def clean_password2(self):
		if 'password1' in self.cleaned_data:
			password1 = self.cleaned_data['password1']
			password2 = self.cleaned_data['password2']
			if password1 == password2:
				return password2
		raise forms.ValidationError('비밀번호가 일치하지 않습니다.')


	def clean_username(self):
		username = self.cleaned_data['username']
		if not re.search(r'^\w+$', username):
			raise forms.ValidationError('사용자 이름은 알파벳, 숫자, 밑줄(_)만 가능합니다.')

		try:
			User.objects.get(username=username)
		except ObjectDoesNotExist:
			return username
		raise forms.ValidationError('이미 사용중인 사용자 이름입니다.')
			


class AccountForm(ModelForm):
	class Meta:
		model = Account

class TestForm(ModelForm):
    class Meta:
        model = Account

    acct_code = forms.CharField(max_length=8)
    acct_name = forms.CharField(max_length=50)
    use_yn = forms.BooleanField
    cdate = forms.DateField
    udate = forms.DateField
    ddate = forms.DateField
    


